package com.example.emos.workflow.config;

import com.example.emos.workflow.service.MeetingService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: //todo
 * @Author: 张广焱
 * @Date: 2024-05-26 下午7:33
 */
@Component
public class MeetingWorkflowJob extends QuartzJobBean {
    private static final Logger log = LoggerFactory.getLogger(MeetingWorkflowJob.class);
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private MeetingService meetingService;

    @Override
    protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
        //获取创建定时器传入的参数
        Map map = ctx.getJobDetail().getJobDataMap();
        String uuid = map.get("uuid").toString();
        //获取工作流实例
        String instanceId = map.get("instanceId").toString();
        //判断会议审批是否未结束
        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult();
        if (instance !=null){
            map.put("processStatus","未结束");
            //todo 删除工作流和相关的定时器

            HashMap param = new HashMap();
            param.put("uuid",uuid);
            param.put("status",2);
            //更新会议状态
            meetingService.updateMeetingStatus(param);
            log.debug("会议已失效");

        }

    }
}
